package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.util.Tool;
import jdk.nashorn.internal.objects.NativeUint8Array;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    @Override
    public ResultModel add(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//增加用户之前，需要对密码进行加密：MD5,java自带的加密
        String mingwen = userModel.getPassword();
        String anwen = Tool.encoderByMd5(mingwen);
        userModel.setPassword(anwen);
//        userModel.setPassword(Tool.encoderByMd5(userModel.getPassword()));
//        ResultModel resultModel = new ResultModel();
//        resultModel.setMsg("success");
//        resultModel.setCount(userDao.add(userModel));
//        resultModel.setCode(0);
//        resultModel.setData("");
//        return resultModel;
        return ResultModel.getResultModel(userDao.add(userModel));
    }

    @Override
    public ResultModel delete(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setMsg("success");
//        resultModel.setCount(userDao.delete(userModel));
//        resultModel.setCode(0);
//        resultModel.setData("");
//        return resultModel;
        return ResultModel.getResultModel(userDao.delete(userModel));
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setMsg("success");
//        resultModel.setCount(userDao.getCount(userModel));
//        resultModel.setCode(0);
//        resultModel.setData(userDao.findAll(userModel));
//        return resultModel;
        return ResultModel.getResultModel(userDao.getCount(userModel), userDao.findAll(userModel));
    }

    @Override
    public ResultModel update(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setMsg("success");
//        resultModel.setCount(userDao.update(userModel));
//        resultModel.setCode(0);
//        resultModel.setData(null);
//        return resultModel;
        return ResultModel.getResultModel(userDao.update(userModel));
    }

    @Override
    public ResultModel findById(UserModel userModel) {
//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        resultModel.setMsg("success");
//        resultModel.setCount(0);
//        resultModel.setCode(0);
//        resultModel.setData(userDao.findById(userModel));
//        return resultModel;
        return ResultModel.getResultModel(userDao.findById(userModel));
    }

    @Override
    public ResultModel getCount(UserModel userModel) {

//        ResultModel resultModel = new ResultModel();
//        resultModel.setMsg("success");
//        resultModel.setCount(userDao.getCount(userModel));
//        resultModel.setCode(0);
//        resultModel.setData(null);
//        return resultModel;
        return ResultModel.getResultModel(userDao.getCount(userModel));
    }

    @Override
    public ResultModel login(UserModel userModel) {
        if (!userModel.getCode().equals(userModel.getSessionCode())) {
            return ResultModel.getResultModel("error-code");
        }
        userModel.setPassword(Tool.encoderByMd5(userModel.getPassword()));
        UserModel model = userDao.findByUsernameAndPassword(userModel);
        if (model == null) {
            return ResultModel.getResultModel("error");
        }
        return ResultModel.getResultModel("success");

//        UserDao userDao = new UserImpl();
//        ResultModel resultModel = new ResultModel();
//        UserModel model = userDao.findByUsernameAndPassword(userModel);
//        if (model == null) {
//            return ResultModel.getResultModel("error");
//        }
//        resultModel.setMsg("success");
//        resultModel.setCount(0);
//        resultModel.setCode(0);
//        resultModel.setData(null);
//        return resultModel;
//        return ResultModel.getResultModel("success");
    }

    @Override
    public ResultModel updatePassword(UserModel userModel) {
        return null;
    }


}
